package com.qf.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsCategory;
import com.qf.service.CategoryService;
import com.qf.util.AdminResponseUtil;
import com.qf.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * projectName: shop-manage
 *
 * @author: 元
 * time: 2021/9/15 0015 17:14
 * description:商品管理
 */
@RestController
@CrossOrigin("*")
@RequestMapping("category")
public class AdminCategoryController {

    public static final Logger logger = LoggerFactory.getLogger(AdminCategoryController.class);

    @Autowired
    private CategoryService categoryService;

    //商品类目列表
    @RequestMapping("list")
    public Object list(Integer page, Integer limit,Integer id,String name,String sort, String order){
       //分页查询除数据
        IPage<DtsCategory> categoryIPage = categoryService.findByPage(page,limit,id,name,sort,order);

        Map<String,Object> categoryMap =new HashMap<>();
        categoryMap.put("total",categoryIPage.getTotal());
        categoryMap.put("items",categoryIPage.getRecords());
        logger.debug("[商场管理]分页查询数据为：{}", categoryMap);
        return ResponseUtil.ok(categoryMap);
    }


    //商品一级类目
    @RequestMapping("l1")
    public Object level1(){

        logger.info("[商品类目]级别接口开始调用");
        List<Map> levelList = categoryService.findLevel1();
        logger.debug("[商品类目]级别列表查询数据为{}", levelList);

        return ResponseUtil.ok(levelList);


    }


    //添加商品类目
    @RequestMapping("create")
    public Object create(@RequestBody DtsCategory dtsCategory){

        String categoryName = dtsCategory.getName();
        if (StringUtils.isEmpty(categoryName)){
            logger.warn("[添加类目]接收到的类目名为null,无法添加!");
            return ResponseUtil.fail();
        }

        boolean hasCategoryName = categoryService.checkOutCategoryName(dtsCategory.getName());
        if (hasCategoryName){
            logger.warn("[商品类目添加]要添加的类目已经存在!", categoryName);
            return AdminResponseUtil.fail();
        }

        dtsCategory = categoryService.saveCategory(dtsCategory);
        logger.info("[商品类目添加]接口调用结束，获取回显数据为：{}", dtsCategory);
        return ResponseUtil.ok(dtsCategory);


    }

    //编辑商品类目信息
    @RequestMapping("update")
    public Object update(@RequestBody DtsCategory dtsCategory){

        int rows = categoryService.updateCategory(dtsCategory);
        if (rows ==0){
            return  ResponseUtil.fail();
        }

        return ResponseUtil.ok();
    }

    //删除商品类目
    @RequestMapping("delete")
    public Object delete(@RequestBody DtsCategory dtsCategory){
        logger.debug("[删除商品类目]传入的id为:{}", dtsCategory.getId());
        int rows = categoryService.removeById(dtsCategory.getId());
        if (rows ==0){
            return  ResponseUtil.fail();
        }
        logger.info("[删除商品类目]删除影响行数为:{}", rows);
        return ResponseUtil.ok();
    }









}
